function geocode(data){
	var location = data.where.value;
	if(location!=""){
	var geocoder = new GClientGeocoder();
	geocoder.getLocations(
	    location,
	    function(response) {
	      if (!response || response.Status.code!="200") {
	        data.status.value = 0;
	      } else {
			data.status.value = 200;
			var place = response.Placemark[0];
	        data.geocode_point.value = place.Point.coordinates[1] + "," + place.Point.coordinates[0];
			
			data.street.value = place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
			data.city.value = place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
			data.state.value = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
			data.where.value = place.address;
	      }
	    }
	  );
}}

function allDay(data){
	if(data.aday.checked==true){ 
		data.aday.value='all_day'; 
		$('#stime').html("");
		$('#etime').html("");
		
	}
	else if(data.aday.checked==false){ 
		data.aday.value='all_day';
		$('#stime').html("<input type='text' name='startTime' id='startTime' class='startTime' autocomplete='off' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>");
		$('#etime').html("<input type='text' name='endTime' id='endTime' class='endTime' autocomplete='off' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>");
		$("#startTime, #endTime").timePicker({show24Hours:false});

		// Store time used by duration.
		var oldTime = $.timePicker("#startTime").getTime();

		// Keep the duration between the two inputs.
		$("#startTime").change(function() {
		  if ($("#endTime").val()) { // Only update when second input has a value.
		    // Calculate duration.
		    var duration = ($.timePicker("#endTime").getTime() - oldTime);
		    var time = $.timePicker("#startTime").getTime();
		    // Calculate and update the time in the second input.
		    $.timePicker("#endTime").setTime(new Date(new Date(time.getTime() + duration)));
		    oldTime = time;
		  }
		});
	}

}

function repeat_amount_daily(data){
	if(data.repeat_days.value!=1) $('#amount_day').html("Every " + data.repeat_days.value + " days");
	else $('#amount_day').html("Daily");
}

function repeat_amount_weekly(data){
	if(data.repeat_weeks.value!=1) $('#amount_day').html("Every " + data.repeat_weeks.value + " weeks");
	else $('#amount_day').html("Weekly");
}

function repeat_amount_monthly(data){
	if(data.repeat_months.value!=1) $('#amount_day').html("Every " + data.repeat_months.value + " months");
	else $('#amount_day').html("Monthly");
}

function repeat_amount_yearly(data){
	if(data.repeat_years.value!=1) $('#amount_day').html("Every " + data.repeat_years.value + " years");
	else $('#amount_day').html("Yearly");		
}

function repeat_on_days(){
	var data = document.getElementsByName("days_of_week[]");
	var a = " on";
	var temp = "";
	for(i=0;i<=6;i++){
		if(data[i].checked==true) temp += " " + data[i].value
	}
	if(temp.indexOf("Monday Tuesday Wednesday Thursday Friday")!="-1" && temp.indexOf("Sunday")=="-1" && temp.indexOf("Saturday")=="-1") temp = " weekdays";
	else if(temp.indexOf("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")!="-1") temp = " all days";
	a += temp;
	$('#days_on').html(a);
}

function dateChanged(data){
	
	if(data.repeat_when.value=='6'){
		repeat_on_months();
	}
}

function repeat_on_months(){
	repeatBy = document.getElementsByName("repeat_how");
	var startDate = new Date(document.getElementById("startDay").value);
	if(repeatBy[0].checked==true){ //repeat by date
		$('#month_on').html(" on day " + startDate.getDate());
	}
	else{ //repeat by day
		//startDate.setTime(Date.parse(startDate.toString()));
		var firstDayOfMonth = new Date();
		firstDayOfMonth.setYear(startDate.getFullYear());
		firstDayOfMonth.setMonth(startDate.getMonth());
		firstDayOfMonth.setDate(1);
		firstDayOfMonth = firstDayOfMonth.getDay();
		var startDay = startDate.getDay();
		var numberOfDays = 0;
		if(startDay>=firstDayOfMonth) numberOfDays = Math.floor(startDate.getDate()/7 + 1);
		else numberOfDays = Math.ceil(startDate.getDate()/7);
		var day ='';
		switch(numberOfDays){
			case 1:
				numberOfDays = 'first';
				break;
			case 2:
				numberOfDays = 'second';
				break;
			case 3:
				numberOfDays = 'third';
				break;
			case 4:
				numberOfDays = 'forth';
				break;
			case 5:
				numberOfDays = 'fifth';
				break;
			case 6:
				numberOfDays = 'sixth';
				break;
			default:
				numberOfDays = '';
		}
		switch(startDate.getDay()){
			case 0:
				day = 'Sunday';
				break;
			case 1:
				day = 'Monday';
				break;
			case 2:
				day = 'Tuesday';
				break;
			case 3:
				day = 'Wednesday';
				break;
			case 4:
				day = 'Thursday';
				break;
			case 5:
				day = 'Friday';
				break;
			case 6:
				day = 'Saturday';
				break;
			default:
				day = '';
		}
		$('#month_on').html(" on the " + numberOfDays + " " + day);
	}
}

function repeat_until(data){
	//alert(data.repeat_end[0].checked);
/*	if(data.repeat_end[0].checked){
		$('#ending_on').html("");
		$('#amount_until').html("");
	}
	else{
		$('#ending_on').html("<input type='text' class='date-pick2' name='end_repeating_date' id='end_repeating_date' onChange='repeat_ending_on(this.form)' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>");
		$('.date-pick2').datePicker({clickInput:true,createButton:false}).val(new Date().asString()).trigger('change');
		var dtm = new Date(data.end_repeating_date.value);
		dtm = dtm.toString().substring(0,16);
		$('#amount_until').html(" until " + dtm);
	}
	$('.date-pick2').datePicker({clickInput:true,createButton:false}).val(new Date().asString()).trigger('change');
	var dtm = new Date(data.end_repeating_date.value);
	dtm = dtm.toString().substring(0,16);
	$('#amount_until').html(" until " + dtm);*/
}
	
function repeat_ending_on(data){
	var dtm = new Date(data.end_repeating_date.value);
	dtm = dtm.toString().substring(0,16);
	$('#amount_until').html(" until " + dtm);
	repeat_end = data.end_repeating_date.value;
}

function repeat_main(){
	var data = document.event;
	var repeat_end = '';
	if(document.event.repeat_when.value=='0'){
		$('#repeat_here').html("");
	}
	else if(data.repeat_when.value=='1'){ //repeating daily
		$('#repeat_here').html("<span style='color:#fff;background:#60451A;margin:30px;padding:2px;'><span id='amount_day'>Daily</span><span id='amount_until'></span></span>\
			<div style='display:block; margin:5px;'>Repeat every\
			<select name='repeat_days' onChange='repeat_amount_daily(this.form)'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option><option value='13'>13</option><option value='14'>14</option></select> days\
			</div><span style='display:block'>Starts: <input type='text' disabled='disabled' value='" + data.startDay.value + "' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>Ends: <span id='ending_on'><input type='text' class='date-pick2' name='end_repeating_date' id='end_repeating_date' onChange='repeat_ending_on(this.form)' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/></span></span>\
		");
	}
	else if(data.repeat_when.value=='2'){ //repeating every weekday
		$('#repeat_here').html("<span style='color:#fff;background:#60451A;margin:30px;padding:2px;'><span id='amount_day'>Weekly on weekdays</span><span id='amount_until'></span></span>\
			<span style='display:block; margin:5px;'>Starts: <input type='text' disabled='disabled' value='" + data.startDay.value + "' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>Ends: <span id='ending_on'><input type='text' class='date-pick2' name='end_repeating_date' id='end_repeating_date' onChange='repeat_ending_on(this.form)' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/></span></span>\
		");
	}
	else if(data.repeat_when.value=='3'){ //repeating M W F
		$('#repeat_here').html("<span style='color:#fff;background:#60451A;margin:30px;padding:2px;'><span id='amount_day'>Weekly on Monday, Wednesday, & Friday</span><span id='amount_until'></span></span>\
			<span style='display:block; margin:5px;'>Starts: <input type='text' disabled='disabled' value='" + data.startDay.value + "' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>Ends: <span id='ending_on'><input type='text' class='date-pick2' name='end_repeating_date' id='end_repeating_date' onChange='repeat_ending_on(this.form)' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/></span></span>\
		");
	}
	else if(data.repeat_when.value=='4'){ //repeating T Th
		$('#repeat_here').html("<span style='color:#fff;background:#60451A;margin:30px;padding:2px;'><span id='amount_day'>Weekly on Tuesday & Thursday</span><span id='amount_until'></span></span>\
			<span style='display:block; margin:5px;'>Starts: <input type='text' disabled='disabled' value='" + data.startDay.value + "' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>Ends: <span id='ending_on'><input type='text' class='date-pick2' name='end_repeating_date' id='end_repeating_date' onChange='repeat_ending_on(this.form)' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/></span></span>\
		");
	}
	else if(data.repeat_when.value=='5'){ //repeating weekly
		var date = new Date(data.startDay.value);
		date = date.toString().substring(0,3);
		days_of_week = document.getElementsByName("days_of_week[]");
		switch(date){
			case 'Sun':
				date = '0';
				break;
			case 'Mon':
				date = '1';
				break;
			case 'Tue':
				date = '2';
				break;
			case 'Wed':
				date = '3';
				break;
			case 'Thu':
				date = '4';
				break;
			case 'Fri':
				date = '5';
				break;
			case 'Sat':
				date = '6';
				break;
			default:
				date = '1';
		}
		
		$('#repeat_here').html("<span style='color:#fff;background:#60451A;margin:30px;padding:2px;'><span id='amount_day'>Weekly</span><span id='days_on'></span><span id='amount_until'></span></span>\
			<div style='display:block; margin:5px;'>Repeat every\
			<select name='repeat_weeks' onChange='repeat_amount_weekly(this.form)'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option><option value='13'>13</option><option value='14'>14</option></select> week\
			</div><div style='display:block;'>Repeat On:<br />\
			<input type='checkbox' name='days_of_week[]' value='Sunday' onChange='repeat_on_days()'/>S\
			<input type='checkbox' name='days_of_week[]' value='Monday' onChange='repeat_on_days()'/>M\
			<input type='checkbox' name='days_of_week[]' value='Tuesday' onChange='repeat_on_days()'/>T\
			<input type='checkbox' name='days_of_week[]' value='Wednesday' onChange='repeat_on_days()'/>W\
			<input type='checkbox' name='days_of_week[]' value='Thursday' onChange='repeat_on_days()'/>Th\
			<input type='checkbox' name='days_of_week[]' value='Friday' onChange='repeat_on_days()'/>F\
			<input type='checkbox' name='days_of_week[]' value='Saturday' onChange='repeat_on_days()'/>Sa\
			</div>\
			<div style ='display:block;>Range<span style='display:block'>Starts: <input type='text' disabled='disabled' value='" + data.startDay.value + "' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>Ends: <span id='ending_on'><input type='text' class='date-pick2' name='end_repeating_date' id='end_repeating_date' onChange='repeat_ending_on(this.form)' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/></span></span>\
		");
		days_of_week[date].checked=true;
		repeat_on_days();
	}
	else if(data.repeat_when.value=='6'){ //repeating monthly
		$('#repeat_here').html("<span style='color:#fff;background:#60451A;margin:8px;padding:2px;'><span id='amount_day'>Monthly</span><span id='month_on'></span><span id='amount_until'></span></span>\
			<div style='display:block;'>Repeat every\
			<select name='repeat_months' onChange='repeat_amount_monthly(this.form)'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option></select> month\
			</div><div style='display:block;'>Repeat By:<br />\
			<input type='radio' name='repeat_how' value='day_of_the_month' onChange='repeat_on_months()'/>day of the month&nbsp;<input type='radio' name='repeat_how' value='day_of_the_week' checked='checked' onChange='repeat_on_months()'/>day of the week</div>\
			Starts: <input type='text' disabled='disabled' value='" + data.startDay.value + "' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>Ends: <span id='ending_on'><input type='text' class='date-pick2' name='end_repeating_date' id='end_repeating_date' onChange='repeat_ending_on(this.form)' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/></span></span>\
		");
		repeat_on_months();
	}
	else if(data.repeat_when.value=='7'){ //repeating yearly
		$('#repeat_here').html("<span style='color:#fff;background:#60451A;margin:8px;padding:2px;'><span id='amount_day'>Yearly</span><span id='month_on'></span><span id='amount_until'></span></span>\
			<div style='display:block;'>Repeat every\
			<select name='repeat_years' onChange='repeat_amount_yearly(this.form)'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option><option value='13'>13</option><option value='14'>14</option></select> year\
			</div><div style='display:block;'>Starts: <input type='text' disabled='disabled' value='" + data.startDay.value + "' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/>Ends: <span id='ending_on'><input type='text' class='date-pick2' name='end_repeating_date' id='end_repeating_date' onChange='repeat_ending_on(this.form)' style='width:100px;margin: 5px 0px 5px 5px;' maxlength='10'/></span></span>\
		");
	}
	if(data.repeat_when.value!='0'){
		if(repeat_end=='') $('.date-pick2').datePicker({clickInput:true,createButton:false}).val(new Date().asString()).trigger('change');
		else $('.date-pick2').datePicker({clickInput:true,createButton:false}).val(repeat_end);
		var dtm = new Date(data.end_repeating_date.value);
		dtm = dtm.toString().substring(0,16);
		$('#amount_until').html(" until " + dtm);
	}
}

Date.format = 'mm/dd/yyyy';
Date.firstDayOfWeek = 7;

$(function(){
	if(!$('#startDay').val()) $('.date-pick').datePicker({clickInput:true,createButton:false}).val(new Date().asString()).trigger('change');
	else $('.date-pick').datePicker({clickInput:true,createButton:false});
	$("#startTime, #endTime").timePicker({show24Hours:false});

	// Store time used by duration.
	var oldTime = $.timePicker("#startTime").getTime();

	// Keep the duration between the two inputs.
	$("#startTime").change(function() {
	  if ($("#endTime").val()) { // Only update when second input has a value.
	    // Calculate duration.
	    var duration = ($.timePicker("#endTime").getTime() - oldTime);
	    var time = $.timePicker("#startTime").getTime();
	    // Calculate and update the time in the second input.
	    $.timePicker("#endTime").setTime(new Date(new Date(time.getTime() + duration)));
	    oldTime = time;
	  }
	});
});
